<template>
	<view>
		<!--自定义导航栏-->
		<uniNavBar title='确认支付' left-text='关闭' fixed='true' status-bar='true' @clickLeft='goBack'></uniNavBar>

		<view class='pay-main'>
			<radio-group @change="onChangePay">
				<label for="" v-for="(item, index) in paymethod" :key="index">
					<view class="pay-item">
						<image class='pay-img' :src='item.imgUrl' mode=""></image>
						<view>
							<view>{{item.name}}支付</view>
							<view class='pay-txt'>{{item.introduce}}</view>
						</view>
						<label class="radio">
							<radio :disabled="item.key=='wxpay'" :value="item.key" color='#FF3333' /><text></text>
						</label>
					</view>
				</label>
			</radio-group>
		</view>
		<!--去支付-->
		<view class='pay-foot'>
			<view class='total'>
				<text class="f-color">合计:</text>
				<text class='total-price'>¥{{details.price}}</text>
			</view>
			<view class="go-pay" @tap='goPayment'>去支付</view>
		</view>

	</view>
</template>

<script>
	import uniNavBar from '@/components/uni/uni-nav-bar/uni-nav-bar.vue'
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		payment
	} from '@/api/order.js'

	export default {
		data() {
			return {
				details: {
					price: 0,
					list: []
				},
				paymethod: [{
						name: '支付宝',
						key: 'alipay',
						introduce: '推荐支付宝用户使用',
						imgUrl: '../../static/img/zfb.png'
					},
					{
						name: '微信',
						key: 'wxpay',
						introduce: '暂不支持微信支付',
						imgUrl: '../../static/img/wxf.png'
					}
				],
				payActive: ''
			}
		},
		computed: {
			...mapState({
				orderNumber: state => state.order.orderNumber
			})
		},
		components: {
			uniNavBar
		},
		onLoad(e) {
			// console.log(this.orderNumber);
			this.details = JSON.parse(e.details);
		},
		methods: {
			...mapMutations(['cleanCart', 'updateOrder']),
			//点击关闭返回上一页
			goBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			onChangePay(e) {
				this.payActive = e.detail.value
			},
			async goPayment() {
				if (!this.payActive) {
					return uni.showToast({
						title: '请选择支付方式',
						duration: 2000,
						icon: 'none'
					});
				}
				// uni.requestPayment({
				// 	provider: this.payActive,
				// 	orderInfo: {}
				// 	success(res) {

				// 	}
				// })

				let res = await payment({
					orderId: this.orderNumber,
					price: this.details.price,
					list: this.details.list
				})
				if (res.paymentUrl) {
					this.updateOrder()

					// uni.navigateTo({
					// 	url:"../payment-success/payment-success"
					// })

					plus.runtime.openURL(res.paymentUrl);
				}

			}
		}
	}
</script>

<style scoped>
	.pay-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx;
		border-bottom: 2rpx solid #CCCCCC;
	}

	.pay-img {
		width: 100rpx;
		height: 100rpx;
	}

	.pay-txt {
		color: #CCCCCC;
	}

	.pay-foot {
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.total {
		padding-left: 100rpx;
		flex: 1;
		background-color: #000000;
		line-height: 100rpx;
	}

	.go-pay {
		color: #FFFFFF;
		background-color: #49BDFB;
		line-height: 100rpx;
		text-align: center;
		width: 220rpx;
	}

	.total-price {
		color: #FFFFFF;
	}
</style>
